// 计算今年生肖的基本年：鼠
let RAT_YEAR = 2020;

// 月份的英文单词
const MONTH_ARR = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']

// 当前日期
let DATE = new Date();
// 当前年
let YEAR = DATE.getFullYear();
// 当前月
let MONTH = DATE.getMonth() + 1;
// 当前日
let DAY = DATE.getDate();
// 当前星期几
let WEEK = DATE.getDay() === 0 ? 7 : DATE.getDay();
// 当前本周周一
function MONDAY() {
    let  daysApart = WEEK - 1;
    let year = ''
    let month = ''
    let day = ''
    if (daysApart < DAY) {
        year = YEAR;
        month = String(MONTH).padStart(2, '0');
        let mondayDay = DAY - daysApart
        day = String(mondayDay).padStart(2, '0');
    } else if (daysApart > day) {
        year = YEAR
        month = String(MONTH - 1).padStart(2, '0');
        switch (MONTH) {
            case 1:
                year = YEAR - 1;
                month = '12'
                day = 31 - (daysApart - DAY)
                break;
            case 2:
            case 4:
            case 6:
            case 8:
            case 9:
            case 11:
                day = 31 - (daysApart - DAY)
                break;
            case 3:
                day = isLeapYear() ? 29 - (daysApart - DAY) : 28 - (daysApart - DAY)
                break;
            case 5:
            case 7:
            case 10:
            case 12:
                day = 30 - (daysApart - DAY)
                break;
        }
    } else {
        return `${YEAR}-${MONTH}-${DAY}`;
    }
    return `${year}-${month}-${day}`;
}
// 当前本周周日
function SUNDAY() {
    if (WEEK === 7) {
        return YEAR + '-' + String(MONTH).padStart(2, '0') + '-' + String(DAY).padStart(2, '0');
    }
    let daysApart = 7 - WEEK;
    let year = ''
    let month = ''
    let day = ''
    let monthRemainderDays = 0
    switch (MONTH) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            monthRemainderDays = 31 - DAY
            break;
        case 2:
            monthRemainderDays = isLeapYear() ? 29 - DAY : 28 - DAY
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            monthRemainderDays = 30 - DAY
            break;
    }
    if (daysApart <= monthRemainderDays) {
        year = YEAR;
        month = String(MONTH).padStart(2, '0');
        let sundayDay = DAY + daysApart
        day = String(sundayDay).padStart(2, '0');
        return `${year}-${month}-${day}`;
    } else {
        year = MONTH === 12 ? YEAR + 1 : YEAR;
        month = String(MONTH + 1).padStart(2, '0');
        day = String(daysApart - monthRemainderDays).padStart(2, '0');
    }
    return `${year}-${month}-${day}`;
}
/**
 * 获取今天是几号
 * @returns {number}
 */
function getToday() {
    return DATE.getDate();
}

/**
 * 获取今天日期
 * @returns {string}
 */
function getTodayDate() {
    return `${YEAR}-${String(MONTH).padStart(2, '0')}-${String(DAY).padStart(2, '0')}`;
}

/**
 * 获取今天所在星期的周一和周日的日期
 * @returns {string[]}
 */
function getWeekStartAndEnd() {
    return [MONDAY(), SUNDAY()]
}

/**
 * 获取本月第一和最后一天
 * @returns {string[]}
 */
function getMonthStartAndEnd() {
    let firstDay = `${YEAR}-${String(MONTH).padStart(2, '0')}-01`
    let lastDay = `${YEAR}-${String(MONTH).padStart(2, '0')}-`
    switch (MONTH) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            lastDay += '31'
            break;
        case 2:
            lastDay += isLeapYear() ? '29' : '28'
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            lastDay += '30'
            break;
    }
    return [firstDay, lastDay];
}

/**
 * 获取当前季节的第一和最后一天
 * @returns {string[]}
 */
function getSeasonsStartAndEnd() {
    let firstDay = ''
    let lastDay = ''
    switch (MONTH) {
        case 1:
        case 2:
            firstDay = `${YEAR - 1}-12-21`;
            lastDay = `${YEAR}-03-20`;
            break;
        case 3:
            if (DAY >= 21) {
                firstDay = `${YEAR}-03-21`;
                lastDay = `${YEAR}-06-20`;
            } else {
                firstDay = `${YEAR - 1}-12-21`;
                lastDay = `${YEAR}-03-20`;
            }
            break;
        case 4:
        case 5:
            firstDay = `${YEAR}-03-21`;
            lastDay = `${YEAR}-06-20`;
            break;
        case 6:
            if (DAY >= 21) {
                firstDay = `${YEAR}-06-21`;
                lastDay = `${YEAR}-09-20`;
            } else {
                firstDay = `${YEAR}-03-21`;
                lastDay = `${YEAR}-06-20`;
            }
            break;
        case 7:
        case 8:
            firstDay = `${YEAR}-06-21`;
            lastDay = `${YEAR}-09-20`;
            break;
        case 9:
            if (DAY >= 21) {
                firstDay = `${YEAR}-09-21`;
                lastDay = `${YEAR}-12-20`;
            } else {
                firstDay = `${YEAR}-06-21`;
                lastDay = `${YEAR}-09-20`;
            }
            break;
        case 10:
        case 11:
            firstDay = `${YEAR}-09-21`;
            lastDay = `${YEAR}-12-20`;
            break;
        case 12:
            if (DAY >= 21) {
                firstDay = `${YEAR}-12-21`;
                lastDay = `${YEAR + 1}-03-20`;
            } else {
                firstDay = `${YEAR}-09-21`;
                lastDay = `${YEAR}-12-20`;
            }
            break;
    }
    return [firstDay, lastDay]
}

/**
 * 获取今年第一和最后一天
 * @returns {string[]}
 */
function getYearStartAndEnd() {
    return [`${YEAR}-01-01`, `${YEAR}-12-31`]
}

/**
 * 是否是闰年
 * @returns {boolean}
 */
function isLeapYear() {
    return YEAR % 4 === 0 && YEAR % 100 !== 0 || YEAR % 400 === 0;
}

/**
 * 获取本月月份
 * @returns {string}
 */
function getMonthEn() {
    return MONTH_ARR[DATE.getMonth()];
}

/**
 * 获取当前是什么季节
 */
function getSeasons() {
    switch (MONTH) {
        case 1:
        case 2:
            return "winter";
        case 3:
            return DAY >= 21 ? "spring" : "winter";
        case 4:
        case 5:
            return "spring";
        case 6:
            return DAY >= 21 ? "summer" : "spring";
        case 7:
        case 8:
            return "summer";
        case 9:
            return DAY >= 21 ? "autumn" : "summer";
        case 10:
        case 11:
            return "autumn";
        case 12:
            return DAY >= 21 ? "winter" : "autumn";
    }
}

function getTwelveAnimals() {
    return (YEAR - RAT_YEAR) % 12;
}

export  {
    getToday,
    getMonthEn,
    getSeasons,
    getTwelveAnimals,
    getTodayDate,
    getWeekStartAndEnd,
    getMonthStartAndEnd,
    getSeasonsStartAndEnd,
    getYearStartAndEnd
}